import { observable, action, toJS } from 'mobx';
import { message } from 'antd';
import { WeaForm } from 'comsMobx';
import { WeaTools } from 'ecCom';


import * as SENDFORM_API from '../apis/sendFormApi';

export default class SendFormStore {
  @observable form = new WeaForm();
  @observable conditioninfo = [];
  @observable uploadFieldMap = new Map(); // 上传控件对应键值对   key为控件的domkey,value为datas数组
  @observable uploadFile = false;
  @observable loading = false;

  @action
  setUploadFieldMap = (fieldDomkey, datas = []) => {
    this.uploadFieldMap.set(fieldDomkey, datas);
  }

  @action
  updateFormFields = (params) => {
    this.form.updateFields(params);
  }

  getUploadFieldDatas = domkey => this.uploadFieldMap.get(domkey)

  buildForm(id = 0, groupId = '') {
    let params = { id, groupId };
    this.form = new WeaForm();
    this.conditioninfo = [];
    this.uploadFieldMap = new Map();
    this.loading = true;
    SENDFORM_API.getFormItems(params).then((res) => {
      this.loading = false;
      let file = [];
      if (res.api_status) {
        let copyItems = this.copyArr(res.conditioninfo[0].items);
        let itemLength = copyItems.length;
        toJS(res.conditioninfo).map((c) => {
          c.items.map((field, index) => {
            let newItemLength = copyItems.length;
            if (field.conditionType.toUpperCase() == WeaTools.types.UPLOAD) {
              this.setUploadFieldMap(field.domkey[0], field.datas);
            }
            // 为了美观 把附件放在最后
            if (field.domkey[0] == 'Document_Attachments') {
              if (itemLength > newItemLength) {
                index -= 1;
              }
              copyItems.splice(index, 1);
              file.push(field);
            }
            // 为了美观 把正文放在最后
            if (field.domkey[0] == 'Document_Text') {
              if (itemLength > newItemLength) {
                index -= 1;
              }
              copyItems.splice(index, 1);
              file.unshift(field);
            }
          });
        });

        res.conditioninfo[0].items = [...copyItems, ...file];
        this.form.initFormFields(toJS(res.conditioninfo));
        this.conditioninfo = res.conditioninfo;
      } else {
        message.error(res.api_errormsg);
      }
    });
  }

  saveSendForm(callback, getTableList) {
    let formParams = this.form.getFormParams();
    let formDatas = this.form.getFormDatas();
    const formMap = new Map(Object.entries(formDatas));
    toJS(this.conditioninfo).map((c) => {
      c.items.map((field) => {
        if (field.conditionType.toUpperCase() == WeaTools.types.BROWSER) {
          formParams[field.domkey[0]] = JSON.stringify(formMap.get(field.domkey[0]));
        }
      });
    });
    for (let uploadField of this.uploadFieldMap.entries()) {
      if (uploadField[1].length != 0) {
        uploadField[1][0].loadlink = `${uploadField[1][0].loadlink}&type=odocExchange`;
      }
      formParams[uploadField[0]] = JSON.stringify(uploadField[1]);
    }
    SENDFORM_API.saveSendForm(formParams).then((res) => {
      this.loading = false;
      this.buildForm();
      if (res.api_status) {
        message.success(res.api_message);
        getTableList();
        callback();
      } else {
        message.error(res.api_message);
        getTableList();
        callback();
      }
    });
  }

  copyArr(arr) {
    let res = [];
    for (let i = 0; i < arr.length; i++) {
      res.push(arr[i]);
    }
    return res;
  }
}
